package com.foxire.lambda;

import com.foxire.innerclass3.Student;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.Comparator;

public class LambdaDemo2 {
    public static void main(String[] args) {
        // 目标：用lambda表达式简化实际例子
        test1();
        test2();
    }

    public static void test1() {
        Student[] students = new Student[6];
        students[0] = new Student("张三", 18, 180, "男");
        students[1] = new Student("李四", 19, 175, "男");
        students[2] = new Student("王五", 20, 165, "男");
        students[3] = new Student("赵六", 21, 170, "男");
        students[4] = new Student("孙七", 22, 165, "男");
        students[5] = new Student("周八", 23, 180, "男");
//简化排序
//        Arrays.sort(students, new Comparator<Student>() {
//            @Override
//            public int compare(Student o1, Student o2) {
//                return o1.getAge() - o2.getAge();
//            }
//        });

//        Arrays.sort(students,(Student o1, Student o2) -> {
//            return o1.getAge() - o2.getAge();
//        });

//        Arrays.sort(students,( o1,  o2) -> {
//            return o1.getAge() - o2.getAge();
//        });

        Arrays.sort(students,(o1,  o2) -> o1.getAge() - o2.getAge());

        // 遍历数组中的对象并输出
        for (int i = 0; i < students.length; i++) {
            System.out.println(students[i]);
        }
    }

    public static void test2() {
        // 目标：搞清楚几个匿名函数内部类的使用场景
        // 需求：创建一个登录窗口，窗口只有一个登录按钮
        JFrame win = new JFrame("登录窗口");
        win.setSize(300, 200);
        win.setLocationRelativeTo(null);// 居中显示
        win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        win.add(panel);

        JButton btn = new JButton("登录");
        panel.add(btn);
// 简化按钮点击事件监听器
//        btn.addActionListener(new ActionListener() {
//            @Override
//            public void actionPerformed(ActionEvent e) {
//                System.out.println("登录成功");
//            }
//        });

//        btn.addActionListener((ActionEvent e)-> {
//            System.out.println("登录成功");
//        });

//       btn.addActionListener( (e)-> {
//           System.out.println("登录成功");
//       });

        btn.addActionListener(e -> System.out.println("登录成功"));

        win.setVisible(true);
    }

}
